<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>客户管理</title>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card card-box-shadow">
    	<div class="layui-card-body">
	        <div class="layui-form toolbar">
	            <div class="layui-form-item">
	                <div class="layui-inline">
	                    <label class="layui-form-label w-auto">客户名称：</label>
	                    <div class="layui-input-inline mr0">
	                        <input id="nameSearch" name="name" class="layui-input search_key" type="search" placeholder="客户名称"/>
	                    </div>
	                </div>
	                <div class="layui-inline">
	                    <button class="layui-btn icon-btn" lay-filter="searchSubmit" id="searchSubmit" lay-submit>
	                        <i class="layui-icon">&#xe615;</i>搜索
	                    </button>
	                    <% if(so.hasPermission("customer:add")){ %>
	                    <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
	                    <% } %>
	                </div>
	            </div>
	        </div>
        	<table class="layui-table" id="customerTable" lay-filter="customerTable"></table>
        </div>
    </div>
</div>

<!--二维码窗口-->
<script type="text/html" id="modelQrcode">
    <div class="layui-row">
        <div id="qrcode" style="margin: 0 auto;margin-top: 40px; width: 300px;"></div>
        <p id="carNoP" style="margin-top: 15px; text-align: center;"></p>
        <!--<input id="qrcode" name="qrcode" type="text" class="layui-input"  autocomplete="off"
        lay-verType="tips" lay-verify="" placeholder="请输入二维码"/>-->
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="qrcodeView">查看二维码</a>
<!--	<% if(so.hasPermission("customer:update")){ %>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <% } %>-->
    <% if(so.hasPermission("customer:delete")){ %>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <% } %>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelCustomer">
    <form id="modelCustomerForm" lay-filter="modelCustomerForm" class="layui-form model-form">
        <input name="id" id="id" type="hidden"/>
        <input name="companyId" type="hidden"/>
    <div class="layui-form-item layui-row">
        <div class="layui-col-md12">
            <label class="layui-form-label">客户名称</label>
            <div class="layui-input-block">
                <input id="name" name="name" type="text" class="layui-input"  autocomplete="off"
                lay-verType="tips" lay-verify="" placeholder="请输入客户名称"/>
            </div>
        </div>

    </div>

    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
        <button class="layui-btn" lay-filter="modelCustomerSubmit" lay-submit>保存</button>
    </div>
    </form>
</script>

<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script type="text/javascript">
    layui.use(['layer', 'form', 'formX', 'table', 'util', 'admin', 'zTree', 'tableX','laydate', 'QRCode'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tableX = layui.tableX;
        var formX = layui.formX;
        var laydate = layui.laydate;

       /* admin.req('customer/getminiqrQr', {
            username: 'xxx',
            password: '123456'
        }, function(res){
            console.log(res);
            customerXm.update({
                data: res.data,
                autoRow: true,
            })
        }, 'post');
        */

        // 渲染表格
        var insTb = tableX.render({
            elem: '#customerTable'
            ,url: 'customer/list'
            ,page: true
            ,toolbar: false
            ,cellMinWidth: 100
            ,cols: [[
                {type: 'numbers'}
                ,{field: "name", minWidth: "60", title: "客户名称", sort: false, hide:false }
                ,{field: "createTime", minWidth: "60", title: "创建时间", sort: false, hide:true ,templet: function (d) {
                        return util.toDateString(d.createTime);
                    }
                }
                ,{field: "createUser", minWidth: "60", title: "创建人", sort: false, hide:true }
                ,{field: "updateTime", minWidth: "60", title: "更新时间", sort: false, hide:true ,templet: function (d) {
                        return util.toDateString(d.createTime);
                    }
                }
                ,{field: "updateUser", minWidth: "60", title: "更新人", sort: false, hide:true }
                ,{field: "del", minWidth: "60", title: "del(0正常 1删除)", sort: false, hide:true }
                ,{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
            ]]
        });

        //监听行双击
        table.on('rowDouble(customerTable)', function(obj){
            var data = obj.data;
            showEditModel(data);
            // obj.tr.find("a[lay-event=edit]").trigger("click");//会跳出来两个一样的框 这个不好用
            //obj.tr.toggleClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行
        });

        // 添加
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(searchSubmit)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
        });

        $('.search_key').bind('keypress',function(event){
            if(event.keyCode == "13")
            {
                $("#searchSubmit").click();
            }
        });

        // 工具条点击事件
        table.on('tool(customerTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            } else if (layEvent === 'qrcodeView') { // 查看二维码
                qrcodeView(obj);
            }
        });

        function qrcodeView(obj) {
            //页面层-自定义
            admin.open({
                type: 1,
                title: '二维码',
                closeBtn: 1,
                area:["500px","500px"],
                shadeClose: true,
                // skin: 'yourclass',
                content: $('#modelQrcode').html(),
                success: function(layero, dIndex) {
                    let QRCode = layui.QRCode;
                    // 二维码
                    let demoQrCode = new QRCode(document.getElementById("qrcode"), {
                        text: "Hello Word!",
                        width: 300,  // 宽度
                        height: 300,  // 高度
                        colorDark: "#000000",  // 颜色
                        colorLight: "#ffffff",  // 背景颜色
                        correctLevel: QRCode.CorrectLevel.H
                    });
                    // 更换内容
                    demoQrCode.makeCode("https://francjc.com:4331/verifyfile/?custom="+obj.data.name);
                    $("#carNoP").html(obj.data.name);
                }
            });
        }

        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”客户吗？', {
                shade: .1,
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.post('customer/delete', {
                    ids: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                        //obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        // 显示编辑弹窗
        function showEditModel(mCustomer) {
            admin.open({
                type: 1,
                maxmin:true,
                closeBtn:1,
                resize: true,
                area:["500px","300px"],
                title: (mCustomer ? '修改' : '添加') + '客户管理',
                content: $('#modelCustomer').html(),
                success: function (layero, dIndex) {
                    var url = mCustomer ? 'customer/update' : 'customer/add';
                    form.val('modelCustomerForm', mCustomer);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelCustomerSubmit)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
						$("button[lay-filter='modelCustomerSubmit']").attr("disabled","disabled");
                        setTimeout(function(){$("button[lay-filter='modelCustomerSubmit']").removeAttr("disabled");}, 1000 );
                        return false;
                    });
                }
            });
        }

    });
</script>
</body>
</html>
